package main

import "fmt"

type Context struct {
	handlers []func(c *Context)
	index    int8
}

func (c *Context) Use(f func(c *Context)) {
	c.handlers = append(c.handlers, f)
}

func (c *Context) Get(path string, f func(c *Context)) {
	c.handlers = append(c.handlers, f)
}

func (c *Context) Next() {
	c.index++
	c.handlers[c.index](c)
}

func (c *Context) Run() {
	c.handlers[0](c)
}

func main() { //剥洋葱
	c := &Context{}
	c.Use(Middle1())
	c.Use(Middle2())
	c.Get("/", func(c *Context) {
		fmt.Println("get handlers function")
	})
	c.Run()
}

func Middle1() func(c *Context) {
	return func(c *Context) {
		fmt.Println("midll1...")
		c.Next()
		fmt.Println("middl1...end")
	}
}

func Middle2() func(c *Context) {
	return func(c *Context) {
		fmt.Println("midll2...")
		c.Next()
		fmt.Println("middl2...end")
	}
}
